package com.leet.code.lcof;

import com.algorithm.model.ListNode;

public class Code06 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode head3 = new ListNode(3);
        ListNode head2 = new ListNode(2);
        head.next = head3;
        head3.next = head2;

        for (int i : reversePrint(head)) {
            System.out.print(i + ",");
        }
        System.out.print("\b");

//        int[] ints = {1, 3, 4, 5, 6, 7,};
//        int[] nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 11};
//
//        copy(ints, nums, 3, 2);

    }

    public static int[] reversePrint(ListNode head) {

        int[] ints = new int[32];
        int[] result = new int[0];
        int i = ints.length;
        while (head != null) {
            i--;
            ints[i] = head.val;
            head = head.next;

            if (i == 0) {
                i = ints.length;
                result = copy(result, ints, 0, ints.length);
            }
        }
        if (i != ints.length) {
            result = copy(result, ints, i, ints.length - i);
        }

        return result;

    }

    public static int[] copy(int[] origin, int[] ints, int start, int length) {
        int[] result = new int[origin.length + length];
        int i = 0;
        for (; i < length; i++) {
            result[i] = ints[i + start];
        }

        for (; i < result.length; i++) {
            result[i] = origin[i - length];
        }
        return result;
    }

}
